package cn.tedu.person.controller;

import cn.tedu.person.pojo.dto.AdminInsertDTO;
import cn.tedu.person.pojo.dto.AdminUpdateDTO;
import cn.tedu.person.pojo.dto.StaffInsertDTO;
import cn.tedu.person.pojo.dto.StaffUpdateDTO;
import cn.tedu.person.pojo.vo.AdminIndexVO;
import cn.tedu.person.pojo.vo.AdminStandardVO;
import cn.tedu.person.pojo.vo.StaffIndexVO;
import cn.tedu.person.pojo.vo.StaffStandardVO;
import cn.tedu.person.service.IAdminService;
import cn.tedu.person.service.IStaffService;
import cn.tedu.person.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/staff")
@Api(tags ="05.员工管理" )
public class StaffController {

    @Autowired
    private IStaffService staffService;

    @ApiOperation("添加员工")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult insert(
            @Valid StaffInsertDTO staffInsertDTO){
        System.out.println(staffInsertDTO);
        log.info("开始处理【添加】的请求，参数：{}",
                staffInsertDTO);
        staffService.addNew(staffInsertDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据父级查询自己列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("/list-by-leader")
    public JsonResult list(Long leaderId){
        List<StaffStandardVO> list = staffService.listByLeaderId(leaderId);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据Id删除信息")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min=1,message = "删除失败，ID值无效！")
                                   @PathVariable Long id){
        staffService.delete(id);
        log.info("删除成功 id={}",id);
        return JsonResult.ok();
    }

    @ApiOperation("根据Id查询信息")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@Range(min=1,message = "查询失败，ID值无效！")
                             @PathVariable Long id){
        StaffStandardVO queryResult = staffService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @ApiOperation("修改员工信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateInfoById(@Range(min=1,message = "修改失败，ID值无效！")
                             @PathVariable Long id,
                                     @Valid StaffUpdateDTO staffUpdateDTO){
        log.debug("开始处理【修改员工详情】的请求，ID：{}，新数据：{}",
                id, staffUpdateDTO);
        staffService.updateInfoById(id,staffUpdateDTO);
        return JsonResult.ok();
    }

    //管理层身份
    @PostMapping("/{id:[0-9]+}/Leader")
    @ApiOperation("管理层类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setLeader(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【管理层身份】的请求，参数：{}", id);
        staffService.setLeader(id);
        return JsonResult.ok();
    }

    //普通员工身份
    @PostMapping("/{id:[0-9]+}/NoLeader")
    @ApiOperation("普通员工类别")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setNoLeader(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【普通员工】的请求，参数：{}", id);
        staffService.setNoLeader(id);
        return JsonResult.ok();
    }

}
